package android.gpsmessenger;

import java.util.*;

public class ContactList 
{
	private Contact _sosContact;
	private SimpleContactList _helpContacts;
	private SimpleContactList _friendContacts;

	// constructors
	ContactList()
	{
		this(new Contact("sos", "411", "411", "sos"));
	}
	
	ContactList(Contact sosContact)
	{
		_sosContact = new Contact(sosContact);
		_helpContacts = new SimpleContactList();
		_friendContacts = new SimpleContactList();
	}

	ContactList(ContactList contactList)
	{
		this(contactList.getSOSContact());
		
		Iterator<Contact> helpContactIterator = contactList.helpContactsIterator();
		while(helpContactIterator.hasNext())
		{
			Contact hc = (Contact) helpContactIterator.next();
			_helpContacts.add(hc);
		}
		
		Iterator<Contact> friendContactIterator = contactList.friendContactsIterator();
		while(friendContactIterator.hasNext())
		{
			Contact fc = (Contact) friendContactIterator.next();
			_friendContacts.add(fc);
		}		
	}	
	
	// getters
	public Contact getSOSContact()
	{
		return new Contact(_sosContact);
	}
	
	public SimpleContactList getHelpContacts()
	{
		return _helpContacts;
	}
	
	public SimpleContactList getFriendContacts()
	{
		return _friendContacts;
	}	
	
	public Contact getContact(Contact c)
	{
		Iterator<Contact> helpContactIterator = helpContactsIterator();
		while(helpContactIterator.hasNext())
		{
			Contact hc = (Contact) helpContactIterator.next();
			if (hc.equals(c))
				return hc;
		}	
		Iterator<Contact> friendContactIterator = friendContactsIterator();
		while(friendContactIterator.hasNext())
		{
			Contact fc = (Contact) friendContactIterator.next();
			if (fc.equals(c))
				return fc;
		}	
		return null;
	}
	
	// setters
	public void setSOSContact(Contact sosContact)
	{
		_sosContact = new Contact(sosContact);
	}
	
	public void setHelpContacts(SimpleContactList helpContacts)
	{
		_helpContacts = helpContacts;
	}
	
	public void setFriendContacts(SimpleContactList friendContacts)
	{
		_friendContacts = friendContacts;
	}
	
	// other methods

	public Iterator<Contact> helpContactsIterator()
	{
	    return _helpContacts.iterator();
	}
	
	public Iterator<Contact> friendContactsIterator()
	{
	    return _friendContacts.iterator();
	}

	public int size()
	{
		return _helpContacts.size() + _friendContacts.size();
	}
	
	public void add(Contact contact)
	{
		if (contact.getID().equals("help"))
			_helpContacts.add(contact);
		else if (contact.getID().equals("friend"))
			_friendContacts.add(contact);
		else if (contact.getID().equals("sos"))
			setSOSContact(contact);
	}
		
	public void remove(Contact contact)
	{
		if (contact.getID().equals("help"))
			_helpContacts.remove(contact);
		else if (contact.getID().equals("friend"))
			_friendContacts.remove(contact);
	}	
	
	public boolean contains(Contact contact)
	{
		return (_helpContacts.contains(contact) || _friendContacts.contains(contact));
	}

	public String toString()
	{
		return ("SOS Contact: " + _sosContact + "\n" +
				"Help Contacts: " + _helpContacts.toString() + "\n" +
				"Friend Contacts: " + _friendContacts.toString());
	}
	

}

/*
public void addHelpContact(Contact contact)
{
	_helpContacts.add(contact);
}

public void addFriendContact(Contact contact)
{
	_friendContacts.add(contact);
}

public void removeHelpContact(Contact contact)
{
	_helpContacts.remove(contact);
}	

public void removeFriendContact(Contact contact)
{
	_friendContacts.remove(contact);
}	

	public boolean containsHelpContact(Contact contact)
	{
		return _helpContacts.contains(contact);
	}
	
	public boolean containsFriendContact(Contact contact)
	{
		return _friendContacts.contains(contact);
	}
	
		public int helpContactListSize()
	{
		return _helpContacts.size();
	}
	
	public int friendContactListSize()
	{
		return _friendContacts.size();
	}

*/